package com.udaos.modules.product.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.udaos.common.base.Result;
import com.udaos.common.filter.PrivateFilter;
import com.udaos.common.page.DataTableColumn;
import com.udaos.common.page.DataTableOrder;
import com.udaos.models.account.Ud_account;
import com.udaos.models.company.Ud_company;
import com.udaos.models.product.Ud_pay_settings;
import com.udaos.models.product.Ud_product_type;
import com.udaos.models.sys.Sys_user;
import com.udaos.modules.account.services.UdAccountService;
import com.udaos.modules.base.utils.EnumsUtils;
import com.udaos.modules.base.vo.SelectVo;
import com.udaos.modules.company.services.UdCompanyService;
import com.udaos.modules.product.services.UdPaySettingsService;
import com.udaos.modules.product.services.UdProductTypeService;

/**
 * 支付设定
 * @author mutaotao
 *
 */
@IocBean
@At("/platform/paysetting")
@Filters({ @By(type = PrivateFilter.class)})
public class UdPaySettingsController {
	
	@Inject
	private UdPaySettingsService paySettingsService;
	@Inject
	private UdCompanyService companyService;
	@Inject
	private UdAccountService accountService;
	@Inject
	private UdProductTypeService productTypeService;
	
	/**
	 * 首页
	 */
	@At("")
	@Ok("beetl:/platform/base/product/pay_setting_index.html")
	@RequiresAuthentication
	public void index() {

	}

	/**
	 * 加载数据
	 * @param length
	 * @param start
	 * @param draw
	 * @param order
	 * @param columns
	 * @return
	 */
	@At
	@Ok("json:full")
	@RequiresAuthentication
	public Object data(@Param("length") int length, @Param("start") int start, @Param("draw") int draw, @Param("::order") List<DataTableOrder> order, @Param("::columns") List<DataTableColumn> columns) {
		return paySettingsService.init(length, start, draw);
		//return paySettingsService.data(length, start, draw, order, columns, cnd, null);
		
	}
	
	/**
	 * 新增
	 * @return
	 */
	@At
	@Ok("beetl:/platform/base/product/pay_setting_add.html")
	@RequiresAuthentication
	public Object add() {
		Map<String,Object> map=new HashMap<String,Object>();
		List<Ud_company> companyList= companyService.find();
		map.put("companyList", companyList);
		//获取已用场景
		List<SelectVo> appSceneList=EnumsUtils.getAppSceneEnum();
		map.put("appSceneList", appSceneList);
		List<Ud_product_type> productTypeList=productTypeService.query();
		map.put("productTypeList", productTypeList);
		return map;
	}
	/**
	 * 新增
	 * @param product
	 * @param req
	 * @return
	 */
	@At
	@Ok("json")
	@RequiresAuthentication
	public Object addDo(@Param("..") Ud_pay_settings pay_settings, HttpServletRequest req) {
		try {
			//设置创建时间
			pay_settings.setCreateAt((int)(System.currentTimeMillis()/1000));
			//设置创建人
			Subject subject = SecurityUtils.getSubject();
            Sys_user user = (Sys_user) subject.getPrincipal();
            pay_settings.setCreateBy(user.getId());
            paySettingsService.insert(pay_settings);
	        return Result.success("保存成功");
	    } catch (Exception e) {
	    	return Result.error(e.getMessage());
	    }
	}
	
	@At
	@Ok("json")
	@RequiresAuthentication
	public Object getAccountWithType(String app_scene){
		Cnd cnd=Cnd.NEW();
		cnd.and("app_scene", "=", app_scene);
		cnd.and("delFlag", "=", "0");
		return accountService.query(cnd);
	} 
	
	@At
	@Ok("json")
	@RequiresAuthentication
	public Object getAccount(String id){
		return accountService.fetch(id);
	}
	@At
	@Ok("json")
	@RequiresAuthentication
	public Object getAccountCompany(String id){
		Ud_account account= accountService.fetch(id);
		return companyService.fetch(account.getCompany_id());
	}
	@At
	@Ok("json")
	@RequiresAuthentication
	public Object getCompany(String id){
		return companyService.fetch(id);
	}
	@At
	@Ok("json") 
	public Object getProductTypeEnum(){
		return productTypeService.query();
	}
	@At
	@Ok("json") 
	public Object getAppSceneEnum(){
		return EnumsUtils.getAppSceneEnum();
	}

}
